/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;

import java.util.List;

import dex.compiler.model.base.ParseNode;
import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Expression;


/**
 * A case section in a switch statement.
 * 
 * The switch statement in Dex is nothing like the switch statement in C or in Java.
 * The cases do not "fall through".  Instead, more than one test can be listed per
 * case:
 * 
 * <pre>
 * switch (value) {
 *     case 1:
 *         foo()
 *     case 2, 3, 4:
 *         bar()
 *     case 5, 9:
 *         baz()
 *     default:
 *         return -1  
 * }
 * </pre>
 * 
 * As a favor to macros, the switch case tests can actually contain <i>any</i> expression:
 * 
 * <pre>
 * switch (someString) {
 *     case "One":
 *         return 1
 *     case "Two", "Three", "Four":
 *         return 2
 *     case "Seven":
 *         return 3
 *  }
 *  </pre>
 *  
 *  The above will survive parsing but not semantic analysis, although an obvious macro could
 *  be written to convert the switch into nested if/else blocks.
 *
 *  @see dex.compiler.model.statement.SwitchStatement
 */
public class SwitchCase extends ParseNode {
	
	
	/**
	 * The tests for this case.
	 */
	private List<Expression> tests;

	
	/**
	 * The body to execute for this case.
	 */
	private Statement body;

	
	/**
	 * Constructs a new SwitchCase.
	 * 
	 * @param place  the place the case originated
	 * @param tests  the tests that should execute the body
	 * @param body   the body to execute if any one of the tests is correct
	 */
	public SwitchCase(Place place, List<Expression> tests, Statement body) {
		super(place);
		if (tests == null) {
			throw new IllegalArgumentException("tests may not be null");
		}
		if (body == null) {
			throw new IllegalArgumentException("body may not be null");
		}
		this.tests = tests;
		this.body = body;
	}

	/**
	 * The body to execute if this case is true.
	 * 
	 * @return  the body to execute
	 */
	public Statement getBody() {
		return body;
	}


	/**
	 * The test values to check.  If any one matches the switch's 
	 * value, then the body will be executed
	 * 
	 * @return  the tests to check.
	 */
	public List<Expression> getTests() {
		return tests;
	}

	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("\tcase ");
		sb.append(Expression.listToString(tests));
		sb.append("\n");
		sb.append(body);
		return sb.toString();
	}


}
